import { Behaviour, Bitmap, DisplayObject, GameEngine } from "./engine";
import { Point } from "./math";


export function add(a: number, b: number) {
    return a + b;
}

const engine = new GameEngine();
const sceneConfig = {
    type: 'displayobject', properties: {
        x: 0, y: 0, alpha: 1, rotation: 0, scaleX: 1, scaleY: 1
    },
    children: [
        {
            type: 'displayobject', properties: {
                x: 100, y: 0, alpha: 1, rotation: 45, scaleX: 1, scaleY: 1
            }, children: [
                { type: 'rectangle', properties: { x: 150, y: 50, color: '#ff0000', alpha: 0.5 } },
                { type: 'textfield', properties: { x: 300, color: '#ff0000', text: '王泽', alpha: 0.5 } },
                { type: 'textfield', properties: { x: 300, y: 40, text: '数字媒体技术', alpha: 1 } },
                { type: 'bitmap', id: "bitmap-1", properties: { x: 0, y: 0, rotation: 0, source: 'assets/icon.jpg', alpha: 1 } },
            ]
        }
    ]
}



engine.start(['assets/icon.jpg'], sceneConfig);
engine.onStart = () => {
    const bitmap = engine.getDisplayObject('bitmap-1') as Bitmap;
    const move = new Move(60);
    bitmap.addBehaviour(move);
    bitmap.addBehaviour(new DisableOnClick())
    // bitmap.addBehaviour(new Man(60));
}

class Man extends Behaviour {

    private totalTime = 0;

    private initialX: number = 0;

    constructor(private speedX: number) {
        super();

    }

    onStart() {
        this.displayObject.onClick = () => {
            this.displayObject.alpha = .1;
        }
        this.initialX = this.displayObject.x;
    }

    onUpdate(duringTime) {
        this.totalTime += duringTime;
        const s0 = this.initialX;
        const v = this.speedX;
        const t = this.totalTime / 1000;
        const s = s0 + v * t;
        this.displayObject.x = s;
    }
}

class DisableOnClick extends Behaviour {

    onStart() {
        this.displayObject.onClick = () => {
            this.displayObject.alpha = .1;
        }
    }
}


class Move extends Behaviour {
    private totalTime = 0;
    private initialX: number = 0;
    constructor(private speedX: number) {
        super();

    }

    onStart() {
        this.initialX = this.displayObject.x;
    }

    onUpdate(duringTime: number) {
        this.totalTime += duringTime;
        const s0 = this.initialX;
        const v = this.speedX;
        const t = this.totalTime / 1000;
        const s = s0 + v * t;
        this.displayObject.x = s;
    }
}
